/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package towerz.game.level;

import java.io.IOException;
import java.util.Vector;
import javax.microedition.lcdui.Image;
import towerz.system.Utilities;
import towerz.system.xml.XmlNode;

/**
 *
 * @author LegACy
 */
public class Dialog {
	protected Dialog() {
		//Initialize
		m_Wave		= -1;
		m_Current	= 0;
		m_Positions	= new Vector();
		m_Dialog	= new Vector();
	}

	public Dialog(XmlNode xml) {
		//Default
		this();

		//Get wave
		m_Wave = Integer.parseInt(xml.getAttribute("wave"));

		//Get character image path
		m_CharacterImgPath = xml.getAttribute("image");

		//For each children
		for (int i = 0; i < xml.getChildren().size(); i++) {
			//Get child
			XmlNode Child = (XmlNode)xml.getChildren().elementAt(i);

			//Is it dialog?
			if (Child.getNodeName().equals("dialog")) {
				//Get position
				m_Positions.addElement(new Integer(Integer.parseInt(Child.getAttribute("position"))));

				//Create vector
				Vector Lines = new Vector();

				//For each child
				for (int j = 0; j < Child.getChildren().size(); j++) {
					//If line
					if (((XmlNode)Child.getChildren().elementAt(j)).getNodeName().equals("line")) {
						//Insert to vector
						Lines.addElement(((XmlNode)((XmlNode)Child.getChildren().elementAt(j)).getChildren().elementAt(0)).getNodeValue());
					}
				}

				//If not empty
				if (!Lines.isEmpty()) m_Dialog.addElement(Lines);
			}
		}
	}

	public int getWave() {
		return m_Wave;
	}

	public Image getCharacterImg(){
		return m_CharacterImg;
	}

	public void loadCharacterImg(){
		try {
			if(m_CharacterImgPath != null)
				m_CharacterImg = Image.createImage(Utilities.INTERFACE_FOLDER + "/" + m_CharacterImgPath + ".png");
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}

	public void unloadCharacterImg(){
		m_CharacterImg = null;
	}

	public String[] nextDialog() {
		//Variable
		String[] Lines = null;

		//if current dialog is valid
		if (m_Current >= 0 && m_Current < m_Dialog.size()) {
			//Get vector
			Vector LineVector = (Vector) m_Dialog.elementAt(m_Current);
			Lines = new String[LineVector.size()];

			//Copy to array
			for (int i = 0; i < Lines.length; i++) Lines[i] = (String)LineVector.elementAt(i);

			//Next
			m_Current++;
		}

		//Return
		return Lines;
	}

	public int getPosition() {
		//Variable
		int Position = 0;

		//if current dialog is valid
		if (m_Current >= 0 && m_Current < m_Dialog.size()) {
			//Get positiom
			Position = ((Integer)m_Positions.elementAt(m_Current)).intValue();
		}

		//Return
		return Position;
	}

	//Members
	protected int		m_Wave;
	protected int		m_Current;
	protected Vector	m_Positions;
	protected Vector	m_Dialog;
	protected String	m_CharacterImgPath;
	protected Image		m_CharacterImg;
}
